#!/bin/bash
set -e
if [[ "$#" -ne 0 && "$#" -ne 2 ]]; then
    echo 'Install a portable version of NSIS which can build the OpenRCT2 Windows installer.'
    echo ''
    echo 'Usage: install-nsis [-d <destination>]'
    echo ''
    echo '    -d <destination>      download prebuilt zip'
    exit 1
fi

if [[ "$1" == "-d" ]]; then
    echo -e "\033[0;36mDownloading prebuilt NSIS from GitHub...\033[0m"
    curl -sLo nsis.zip "https://github.com/OpenRCT2/Dependencies/releases/download/v20/nsis.zip"
    7z -bd -y "-o$2" x nsis.zip
    rm nsis.zip
    echo -e "\033[0;32mNSIS downloaded, add "$2/bin" to PATH\033[0m"
    exit 0
fi

# Download NSIS with chocolatey and then download extra plugins
nsisdir="C:/ProgramData/chocolatey/lib/nsis.portable"
if [[ -d $nsisdir ]]
then
    echo -e "\033[0;36mNSIS already installed.\033[0m"
    exit 0
fi

echo -e "\033[0;36mDownloading NSIS from chocolatey...\033[0m"
cinst nsis.portable --version=3.01-beta1

echo -e "\033[0;36mDownloading KillProcDLL for NSIS...\033[0m"
curl -sLo nsisxtra.zip "http://nsis.sourceforge.net/mediawiki/images/5/53/KillProcDll%26FindProcDll.zip"
7z x nsisxtra.zip
cp FindProcDLL.dll "$nsisdir/tools/nsis-3.0b1/Plugins/x86-ansi"

echo -e "\033[0;36mDownloading UAC plugin for NSIS...\033[0m"
curl -sLo uac.zip "http://nsis.sourceforge.net/mediawiki/images/8/8f/UAC.zip"
7z x uac.zip
cp UAC.nsh "$nsisdir/tools/nsis-3.0b1/Include"
cp -r Plugins "$nsisdir/tools/nsis-3.0b1"

echo -e "\033[0;32mNSIS installed!\033[0m"
